package com.koron.css2.clientCenter.impl;

import com.koron.css2.clientCenter.bean.SendTemMsgAccBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.secret.SecretReqUtil;
import com.koron.util.secret.StaticInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.Map;

/**
 * 异步调用中台短信接口
 */
@Service
public class AsyncSendMsg {

    public static Logger logger = LoggerFactory.getLogger(AsyncSendMsg.class);

    /**
     * 合同催签发送微信模板消息
     * @param noticeWay
     * @param phraseFieldData
     * @param groupCode
     * @param createUser
     * @return
     */
    @Async
    public MessageBean sendWxMessageToUserContract(Integer noticeWay, Map<String, String> phraseFieldData, String groupCode, String createUser) {
        try {
            String result = sendWxMessage(noticeWay, phraseFieldData.get("userNo"), createUser, groupCode, phraseFieldData);
            return JsonUtils.jsonToPojo(result, MessageBean.class);
        } catch (Exception e) {
            logger.error(phraseFieldData.get("userNo") + "合同催签微信发送失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "error", null);
        }
    }

    /**
     * 发送微信模板消息
     *
     * @param noticeWay
     * @param userNo
     * @param createUser
     * @param curWaterCode
     * @param contentFieldMap
     * @return
     * @throws Exception
     */
    private String sendWxMessage(Integer noticeWay, String userNo, String createUser, String curWaterCode, Map<String, String> contentFieldMap) throws Exception {
        SendTemMsgAccBean sendBean = new SendTemMsgAccBean();
        sendBean.setNoticeType(2);
        sendBean.setNoticeWay(noticeWay);
        sendBean.setUserNo(userNo);
        sendBean.setCreateUser(createUser);
        sendBean.setGroupCode(curWaterCode);
        sendBean.setNoticeContent(JsonUtils.objectToJson(contentFieldMap));
        @SuppressWarnings("rawtypes")
        com.koron.util.secret.RequestBean reqBean
                = new com.koron.util.secret.RequestBean();
        reqBean.setData(sendBean);
        reqBean.setUrl(StaticInfo.templateSendUrl);
        String result = SecretReqUtil.sendToGatWay(reqBean);
        logger.info("result: " + result);
        return result;
    }
}
